<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>部门列表</title>
    <base href="${pageContext.request.contextPath}/">
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<div style="padding: 15px;">
    <blockquote class="layui-elem-quote layui-quote-nm">
            <span class="layui-breadcrumb">
              <a href="">首页</a>
              <a><cite>部门管理</cite></a>
            </span>
    </blockquote>

    <div class="layui-form-item dept-query">
        <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline" style="width: 200px;">
                <input type="text" name="likeName" id="likeName" placeholder="请输入部门名称" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" id="dept-query" data-type="query">查询</button>
        </div>

    </div>

    <table class="layui-hide" id="dept-table" lay-filter="dept-table"></table>
</div>

<!-- 表头工具栏 -->
<script type="text/html" id="thead-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增部门</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDelete">批量删除</button>
    </div>
</script>

<!-- 单元格工具栏 -->
<script type="text/html" id="row-toolbar">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>

<script src="layui/layui.js"></script>
<script>
    layui.use(['element', 'table', 'form', 'layer'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.$;

        var tableIns = table.render({
            elem: '#dept-table'
            , id: 'deptId'
            , method: 'POST'
            , url: '${pageContext.request.contextPath}/admin/dept/list'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                {type: 'checkbox'},
                {field: 'id', title: 'ID', sort: true},
                {field: 'name', title: '部门名'},
                {fixed: 'right', align: 'center', title: '编辑', toolbar: '#row-toolbar'}
            ]]
            , page: {
                limits: [10, 15, 20]
            }
            , even: true
            , toolbar: '#thead-toolbar'
            , defaultToolbar: ['exports', 'print']
        });

        //监听表格复选框选择
        table.on('checkbox(dept-table)', function (obj) {
            //
        });
        //监听行工具条
        table.on('tool(dept-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除' + data.name + '部门么？', function (index) {
                    // 异步删除部门方法
                    $.ajax({
                        url: "${pageContext.request.contextPath}/admin/dept/detail?id=" + data.id,
                        method: "DELETE",
                        success: function (data) {
                            if (data.msg == "true") {
                                layer.msg("部门删除成功！");
                                // 重载表格
                                $("#dept-query").click();
                            } else {
                                layer.msg("部门删除失败！");
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    skin: 'layui-layer-molv',
                    area: ['400px', '200px'],
                    fixed: true, //不固定
                    title: '修改部门',
                    maxmin: true,
                    content: 'admin/dept/detail?id=' + data.id
                });
            }
        });

        //头工具栏事件
        table.on('toolbar(dept-table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchDelete':
                    var depts = checkStatus.data;
                    $.ajax({
                        url: "${pageContext.request.contextPath}/admin/dept/detail?ids=" + depts.map(item => item.id).join(","),
                        method: "DELETE",
                        success: function (data) {
                            if (data.msg == "true") {
                                layer.msg("部门" + depts.map(item => item.name) + "删除成功！");
                                // 重载表格
                                $("#dept-query").click();
                            } else {
                                layer.msg("部门删除失败！");
                            }
                        }
                    });
                    break;
                case 'add':
                    layer.open({
                        type: 2,
                        skin: 'layui-layer-molv',
                        area: ['400px', '200px'],
                        fixed: true, //不固定
                        title: '新增部门',
                        maxmin: true,
                        content: 'admin/dept/detail'
                    });
                    break;
            }
        });

        var active = {
            query: function () { //搜索表单
                tableIns.reload({
                    where: { //设定异步数据接口的额外参数，任意设
                        'likeName': $("#likeName").val()
                    }
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }
        };

        $('.dept-query .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
</body>
</html>
